// pages/index1/index1.js
var WxParse = require('../wxParse/wxParse.js');
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    disabled: false,
    loading: false,

    startupImg:[],
    icon: '../../images/logo.png',
    ulifeUrl: app.myUlife.ulifeUrl,//服务器路径
    wxts:'播州区机关幼儿园登记系统旨在便捷登记流程，透明报名结果。',

    sfkqbm:'',
    bmjzrq:'',
    rxstart:'', 
    rxend:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
  },

  intoBmxt: function () {
    var that = this;
    this.setData({
      disabled: true,
      loading: true
    });
    if (wx.getSetting) {
      wx.getSetting({
        success(res) {
          if (!res.authSetting['scope.userInfo']) {
            wx.authorize({
              scope: 'scope.userInfo',
              success() {
                // 登录
                that.wxlogin();
              }
            })
          } else {
            // 登录
            that.wxlogin();
          }
        }
      })
    } else {
      //不兼容处理
      // 登录
      that.wxlogin();
    }
  },

  // 登录start
  wxlogin: function () {
    var that = this;
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        that.code = res.code;
        that.getwxuserinfo();
      }
    })
  },

  // 发送 res.code 到后台换取 openId, sessionKey, unionId
  getwxuserinfo: function () {
    var that = this;
    wx.getUserInfo({
      success: res => {
        wx.hideLoading(res.userInfo);
        //加入缓存
        wx.setStorageSync('city', res.userInfo.city);
        wx.setStorageSync('province', res.userInfo.province);
        wx.setStorageSync('gender', res.userInfo.gender);
        wx.setStorageSync('nickName', res.userInfo.nickName);
        wx.setStorageSync('avatarUrl', res.userInfo.avatarUrl);

        // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
        // 所以此处加入 callback 以防止这种情况
        if (that.userInfoReadyCallback) {
          that.userInfoReadyCallback(res)
        }
      }
    })

 
    wx.request({
      url: app.myUlife.ulifeUrl + '/wechart/getwxuserinfo.html',//请求地址
      data: {
        code: that.code
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var openid = res.data.openid;
        wx.setStorageSync('openid', openid);//加入缓存

        that.setData({
          disabled: true,
          loading: true
        });
        wx.redirectTo({
          url: '../../pages/wode/wode'
        })
      }
    })
  }

})
